package com.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * 读取配置文件
 * 学习参考：https://blog.csdn.net/liuyunyihao/article/details/81229009
 * 修改配置文件编码:https://blog.csdn.net/fengqing5578/article/details/80648753
 */
public class ReadProperties {
    public static void main(String[] args) {
        //readProperty1();
        readProperty3();
    }

    /**
     * 指定读取
     */
    private static void readProperty1() {
        Properties properties = new Properties();
        InputStream inputStream = Object.class.getResourceAsStream("/code.properties");
        InputStreamReader inputStreamReader ;
        try {
            inputStreamReader = new InputStreamReader(inputStream,"UTF8");
            properties.load(inputStreamReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(properties.get("name"));
        System.out.println(properties.get("pwd"));
        System.out.println(properties.get("address"));
    }

    /**
     *     遍历读取
     */
    private static void readProperty3() {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("code");
        //遍历取值
        Enumeration enumeration = resourceBundle.getKeys();
        while (enumeration.hasMoreElements()) {
            try {
                String value = resourceBundle.getString((String) enumeration.nextElement());
                System.out.println(new String(value.getBytes("iso-8859-1"), "utf8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }
}
